package com.zhisolution.zhicms.admin;

import java.util.List;

import com.jfinal.core.Controller;
import com.zhisolution.zhicms.model.sys_user;
import com.zhisolution.zhicms.util.JsonResult;
import com.zhisolution.zhicms.util.StringCode;
import com.zhisolution.zhicms.util.StringUtil;

//@ActionKey("/login")
public class LoginController extends Controller {
	public void index() {
		sys_user sysUser = (sys_user) getSessionAttr("zhicms_sys_user");
		if (sysUser != null) {
			redirect("/admin/index");
		} else {
			render("/admin/登录.html");
		}
	}

	public void login() {
		if (StringUtil.isNotBlank(getPara("name"))) {
			if (StringUtil.isNotBlank(getPara("password"))) {
				List<sys_user> list = sys_user.dao.searchNameANDPassword(getPara("name"), StringCode.encrypt(getPara("password")));
				if (list != null && list.size() > 0) {
					renderJson(JsonResult.ajax(true, "成功"));
					setSessionAttr("zhicms_sys_user", list.get(0));
				} else {
					renderJson(JsonResult.ajax(false, "账号或密码错误"));
				}
			} else {
				renderJson(JsonResult.ajax(false, "密码不能为空"));
			}
		} else {
			renderJson(JsonResult.ajax(false, "账号不能为空"));
		}
	}

	public void logout() {
		removeSessionAttr("zhicms_sys_user");
		redirect("/admin");
	}

	public void quit() {
		removeSessionAttr("zhicms_sys_user");
		renderJson(JsonResult.ajax(true, "成功"));
	}
}
